import {
  Column,
  Entity,
  Index,
  OneToMany,
  PrimaryGeneratedColumn,
} from "typeorm";
import { Organizations } from "./Organizations";
import { Permissions } from "./Permissions";
import { Roles } from "./Roles";
import { UserRoles } from "./UserRoles";
import { Users } from "./Users";

@Index("code", ["code"], { unique: true })
@Index("uniq_code", ["code"], { unique: true })
@Entity("systems", { schema: "nest-demo" })
export class Systems {
  @PrimaryGeneratedColumn({ type: "int", name: "id", unsigned: true })
  id: number;

  @Column("varchar", {
    name: "code",
    unique: true,
    comment: "系统唯一编码，如 main, crm, erp",
    length: 50,
  })
  code: string;

  @Column("varchar", {
    name: "name",
    comment: "系统名称，如 主站, CRM系统, ERP系统",
    length: 100,
  })
  name: string;

  @Column("varchar", { name: "description", nullable: true, length: 255 })
  description: string | null;

  @Column("tinyint", {
    name: "status",
    comment: "系统状态：1-启用，0-停用",
    width: 1,
    default: () => "'1'",
  })
  status: boolean;

  @Column("timestamp", {
    name: "created_at",
    nullable: true,
    default: () => "CURRENT_TIMESTAMP",
  })
  createdAt: Date | null;

  @Column("timestamp", {
    name: "updated_at",
    nullable: true,
    default: () => "CURRENT_TIMESTAMP",
  })
  updatedAt: Date | null;

  @OneToMany(() => Organizations, (organizations) => organizations.system)
  organizations: Organizations[];

  @OneToMany(() => Permissions, (permissions) => permissions.system)
  permissions: Permissions[];

  @OneToMany(() => Roles, (roles) => roles.system)
  roles: Roles[];

  @OneToMany(() => UserRoles, (userRoles) => userRoles.system)
  userRoles: UserRoles[];

  @OneToMany(() => Users, (users) => users.system)
  users: Users[];
}
